package com.jwgf.smarowindingmachine_unity.logic;

import com.jwgf.group.GroupMap;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.SpindleParameter;

import java.text.DecimalFormat;

/*
    单锭接收和上位机保存的长度数据都是以米为单位，重量以千克为单位，显示时根据长度单位和重量单位转化
 */
public class UnitUtils {

    /**
     * Unit transfer factor, from metre to yard
     */
    public static final float METRE_TO_YARD_FACTOR = 1f / 0.9144f;
    /**
     * Unit transfer factor, from kg to lb
     */
    public static final float KG_TO_LB_FACTOR = 1f / 0.4536f;
    /**
     * Unit transfer factor, from Ne to Nm
     */
    public static final float NE_TO_NM_FACTOR = 0.583f;
    /**
     * Unit transfer factor, Constant of Ne multiplied by tex
     */
    public static final float NE_MULTI_TEX_CONST = 583.1f;

    public static final float LENGTH_TO_PULSE_FACTOR = 0.0099323565f;
    public static final float PULSE_TO_LENGTH_FACTOR = 100.68104166667f;

    private static MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

    // 界面长度转化为存储和发送长度
    public static float getSendLength(float uiLength) {
        float value = 0;
        String unit = sp.getValue(MachineParameterSharedPreference.SYSTEM_LENGTH_UNIT, "0");
        if (unit.equals("0")) {
            value = uiLength;
        } else {
            value = uiLength / METRE_TO_YARD_FACTOR;
        }
        return value;
    }

    public static int getSendLengthFromWeight(float uiWeight, SpindleParameter parameter) {
        int length_value = 0;

        float prod_coef = Float.parseFloat(parameter.productionCorrectFactor);
        float yarn_count = Float.parseFloat(parameter.frame28DC);
        int ends_num = Integer.parseInt(parameter.endsNum);

        float mMetre = uiWeight * (1.71f * yarn_count * ends_num) / prod_coef;
        length_value = Math.round(mMetre);

        return length_value;
    }

    // 存储长度转化为界面长度
    public static String getUILength(String meter) {
        float value = 0;
        String unit = sp.getValue(MachineParameterSharedPreference.SYSTEM_LENGTH_UNIT, "0");

        if (unit.equals("0")) {
            value = Float.parseFloat(meter);
        } else {
            value = Float.parseFloat(meter) * METRE_TO_YARD_FACTOR;
        }

        return String.valueOf(Math.round(value));
    }

    public static String getUIWeightFromLength(int length, SpindleParameter parameter) {
        String value = "";
        DecimalFormat df = new DecimalFormat("0.000");
        float prod_coef = Float.parseFloat(parameter.productionCorrectFactor);
        float yarn_count = Float.parseFloat(parameter.frame28DC);
        int ends_num = Integer.parseInt(parameter.endsNum);
        int gram = Math.round(length * prod_coef / (1.71f * yarn_count * ends_num));
        float weight = getUIWeightFromWeight(gram);
        value = df.format(weight);
        return value;
    }

    /*
    mWeight 单位为g，返回kg
     */
    public static float getUIWeightFromWeight(float mWeight) {
        float value = 0;
        int weight_unit = 0;

        weight_unit = Integer.parseInt(sp.getValue(MachineParameterSharedPreference.SYSTEM_WEIGHT_UNIT, "0"));

        switch (weight_unit) {
            case 0:
                value = mWeight / 1000;
                break;
            case 1:
                value = mWeight / (KG_TO_LB_FACTOR * 1000);
                break;
            default:
                break;
        }

        return value;
    }

    public static float getSendCount(float nCount) {
        float value = 0;
        String unit = sp.getValue(MachineParameterSharedPreference.SYSTEM_COUNT_UNIT, "0");//0:英支   1：公支   2：号

        if (unit.equals("0")) {
            value = nCount;
        } else if (unit.equals("1")) {
            value = nCount * NE_TO_NM_FACTOR;
        } else {
            value = NE_MULTI_TEX_CONST / nCount;
        }
        return value;
    }

    public static float getUICount(float mNe) {
        float value = 0;
        String unit = sp.getValue(MachineParameterSharedPreference.SYSTEM_COUNT_UNIT, "0");

        if (unit.equals("0")) {
            value = mNe;
        } else if (unit.equals("1")) {
            value = mNe / NE_TO_NM_FACTOR;
        } else {
            value = NE_MULTI_TEX_CONST / mNe;
        }
        return value;
    }

    public static int pulse2Length(int pulse, int spindle) {
        long id = GroupMap.getInstance().getSpindleParameterIdFromSpindle(spindle);
        // 防止界面修改数据库时，后台计算崩溃
        if (id == 0)
            return 0;
        SpindleParameter para = ObjectBox.get().boxFor(SpindleParameter.class).get(id);
        int len = (int) (pulse * PULSE_TO_LENGTH_FACTOR / Float.parseFloat(para.slipCoefficient));
        return len;
    }

    public static float length2Weight(int length, int spindle) {
        long id = GroupMap.getInstance().getSpindleParameterIdFromSpindle(spindle);
        // 防止界面修改数据库时，后台计算崩溃
        if (id == 0)
            return 0;
        SpindleParameter para = ObjectBox.get().boxFor(SpindleParameter.class).get(id);
        float weight = (length * Float.parseFloat(para.productionCorrectFactor))
                / (1.71f * Float.parseFloat(para.frame28DC) * Integer.parseInt(para.endsNum) * 1000);
        return weight;
    }

}
